package com.lx.tomcat.server;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public abstract class BaseServer<T> implements Server{

    private ThreadPoolExecutor executorService;

    public BaseServer(){
        TaskQueue taskQueue = new TaskQueue(10);
        executorService = new ThreadPoolExecutor(5,100,1, TimeUnit.SECONDS,taskQueue);
        taskQueue.setExecutor(executorService);
    }

    public void request(T t) {
        executorService.execute(()->{
            try {
                processor(t);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }

    protected abstract void processor(T t) throws IOException;
}
